package com.liang.maogouyuyin.ui.adapter

import android.content.Context
import android.content.Intent
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.liang.maogouyuyin.bean.VarietyBean
import com.liang.maogouyuyin.databinding.ItemVarietyBinding
import com.liang.maogouyuyin.ui.activity.WebActivity
import com.bumptech.glide.Glide

class VarietyAdapter(val context: Context): RecyclerView.Adapter<VarietyAdapter.ViewHolder>() {
    class ViewHolder(binding: ItemVarietyBinding): RecyclerView.ViewHolder(binding.root) {
        val itemImage = binding.itemVarietyImage
        val itemName = binding.itemVarietyName
    }

    private lateinit var list: ArrayList<VarietyBean>

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val binding = ItemVarietyBinding.inflate(LayoutInflater.from(context), parent, false)
        return ViewHolder(binding)
    }

    override fun getItemCount(): Int {
        return list.size
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        val bean = list[position]
        Glide.with(context).load(bean.image).into(holder.itemImage)
        holder.itemName.text = bean.variety
        holder.itemView.setOnClickListener {
            val intent = Intent(context, WebActivity::class.java)
            intent.putExtra("url", bean.url)
            intent.putExtra("title", bean.variety)
            intent.putExtra("type", 0)
            context.startActivity(intent)
        }
    }

    fun setList(list: ArrayList<VarietyBean>) {
        this.list = list
    }
}